home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / PowerMacOberon feb96 / Source / Util.Mod (.txt) < prev    next >
Encoding:
Oberon Text  |  1995-12-08  |  7.7 KB  |  213 lines  |  [TEXT/.Ob4]

  1. Syntax10.Scn.Fnt
  2. StampElems
  3. Alloc
  4. 8 Dec 95
  5. Syntax10b.Scn.Fnt
  6. FoldElems
  7. Syntax10i.Scn.Fnt
  8. MODULE Util; (*
  9. IMPORT Display, Viewers, MenuViewers, TextFrames, Oberon, Texts, Strings, In, Out;
  10.     w: Texts.Writer;
  11. PROCEDURE FindChar*;    
  12. (*finds invisible characters*)
  13.     VAR r: Texts.Reader; v: Viewers.Viewer; f: TextFrames.Frame; ch: CHAR; m: Oberon.ControlMsg; pos, len: LONGINT;
  14. BEGIN
  15.     v := Oberon.FocusViewer;
  16.     IF (v # NIL) & (v IS MenuViewers.Viewer) THEN
  17.         f := v.dsc.next(TextFrames.Frame); pos := f.carloc.pos; len := f.text.len;
  18.         Texts.OpenReader(r, f.text, pos);
  19.         WHILE pos < len DO
  20.             Texts.Read(r, ch); INC(pos);
  21.             IF ((ch < " ") & (ch # 0DX) & (ch # 9X)) OR (ch = 7FX) OR (ch > 85X) THEN
  22.                 Texts.WriteInt(w, ORD(ch), 0); Texts.WriteLn(w); Texts.Append(Oberon.Log, w.buf);
  23.                 m.id := Oberon.neutralize; f.handle(f, m); TextFrames.Show(f, pos-50); TextFrames.SetCaret(f, pos);
  24.                 RETURN
  25.             END
  26.         END;
  27.         TextFrames.RemoveCaret(f)
  28. END FindChar;
  29. PROCEDURE ExtractLines*;    (** [ patternName | patternString | "^"] -- removes lines with pattern from marked viewer *)    
  30.     VAR t: Texts.Text; r: Texts.Reader; pat, line: ARRAY 128 OF CHAR; ll: INTEGER;
  31.         v: Viewers.Viewer; x, y: INTEGER; eof: BOOLEAN; out: TextFrames.Frame; pos: LONGINT;
  32.     PROCEDURE GetPattern(VAR pat: ARRAY OF CHAR);    
  33.         VAR s: Texts.Scanner; t: Texts.Text; beg, end, time: LONGINT;
  34.     BEGIN
  35.         Texts.OpenScanner(s, Oberon.Par.text, Oberon.Par.pos); Texts.Scan(s);
  36.         IF (s.class = Texts.Char) & (s.c = "^") THEN
  37.             Oberon.GetSelection(t, beg, end, time);
  38.             IF time >= 0 THEN Texts.OpenScanner(s, t, beg); Texts.Scan(s) END
  39.         END;
  40.         IF (s.class = Texts.Name) OR (s.class = Texts.String) THEN COPY(s.s, pat) ELSE pat[0] := 0X END
  41.     END GetPattern;
  42.     PROCEDURE GetText(VAR t: Texts.Text);    
  43.     BEGIN
  44.         v := Oberon.MarkedViewer();
  45.         IF (v # NIL) & (v.dsc.next IS TextFrames.Frame) THEN t := v.dsc.next(TextFrames.Frame).text
  46.         ELSE t := NIL
  47.         END
  48.     END GetText;
  49.     PROCEDURE GetLine(VAR s: ARRAY OF CHAR; VAR ll: INTEGER; VAR eof: BOOLEAN);    
  50.         VAR i: INTEGER; ch: CHAR;
  51.     BEGIN i := 0;
  52.         IF ~ eof THEN
  53.             LOOP
  54.                 Texts.Read(r, ch); IF (ch = 0DX) OR (ch = 0X) THEN EXIT END;
  55.                 s[i] := ch; INC(i)
  56.             END;
  57.             eof := ch = 0X
  58.         END;
  59.         s[i] := 0X; ll := i
  60.     END GetLine;
  61.     PROCEDURE WriteString (VAR s: ARRAY OF CHAR);    
  62.         VAR i: INTEGER; ch: CHAR;
  63.     BEGIN
  64.         i := 0; ch := s[0];
  65.         WHILE ch # 0X DO Texts.Write(w, ch); INC(i); ch := s[i] END
  66.     END WriteString;
  67. BEGIN
  68.     GetPattern(pat); GetText(t);
  69.     IF (pat # "") & (t # NIL) THEN
  70.         out := TextFrames.NewText(TextFrames.Text(""), 0);
  71.         Texts.OpenReader(r, t, 0); pos := 0; eof := FALSE;
  72.         LOOP
  73.             GetLine(line, ll, eof); IF eof & (ll = 0) THEN EXIT END;
  74.             IF (ll > 0) & Strings.Match(line, pat) THEN
  75.                 WriteString(line); Texts.WriteLn(w);
  76.                 Texts.Delete(t, pos, pos + ll + 1); Texts.OpenReader(r, t, pos)
  77.             ELSE pos := pos + ll +1
  78.             END
  79.         END;
  80.         Oberon.AllocateUserViewer(0, x, y);
  81.         v := MenuViewers.New(TextFrames.NewMenu(pat, "System.Close"), out, TextFrames.menuH, x, y);
  82.         Texts.Append(out.text, w.buf)
  83. END ExtractLines;
  84. PROCEDURE GetSelection* (VAR F: TextFrames.Frame);    
  85.     VAR time: LONGINT; v: Viewers.Viewer; x: INTEGER; f: TextFrames.Frame;
  86. BEGIN
  87.     time := -1; x := 0; F := NIL;
  88.     WHILE x < Display.Width DO
  89.         v := Viewers.This(x, 0);
  90.         WHILE v.state > 1 DO
  91.             IF v.dsc.next IS TextFrames.Frame THEN
  92.                 f := v.dsc.next(TextFrames.Frame); 
  93.                 IF (f.hasSel) & (f.time > time) THEN F := f; time := f.time END;
  94.             END;
  95.             v := Viewers.Next(v)
  96.         END;
  97.         x := x + v.W
  98. END GetSelection;
  99. PROCEDURE UmlautOM*;    
  100.     VAR v: Viewers.Viewer; t: Texts.Text; r: Texts.Reader; ch: CHAR; pos: LONGINT;
  101. BEGIN
  102.     v := Viewers.This(Oberon.Pointer.X, Oberon.Pointer.Y);
  103.     t := v.dsc.next(TextFrames.Frame).text;
  104.     pos := 0; Texts.OpenReader(r, t, pos);
  105.     LOOP Texts.Read(r, ch); INC(pos);
  106.         IF ch = 0X THEN EXIT END;
  107.         CASE ORD(ch) OF
  108.             128: 
  109.         | 129: Texts.Write(w, CHR(133))
  110.         | 130: Texts.Write(w, CHR(134))
  111.         | 131: Texts.Write(w, CHR(138))
  112.         | 132: Texts.Write(w, CHR(154))
  113.         | 133: Texts.Write(w, CHR(159))
  114.         ELSE
  115.         END;
  116.         IF w.buf.len > 0 THEN
  117.             Texts.Delete(t, pos-1, pos); Texts.Insert(t, pos-1, w.buf); Texts.OpenReader(r, t, pos)
  118.         END
  119. END UmlautOM;
  120. PROCEDURE UmlautMO*;    
  121.     VAR v: Viewers.Viewer; t: Texts.Text; r: Texts.Reader; ch: CHAR; pos: LONGINT;
  122. BEGIN
  123.     v := Viewers.This(Oberon.Pointer.X, Oberon.Pointer.Y);
  124.     t := v.dsc.next(TextFrames.Frame).text;
  125.     pos := 0; Texts.OpenReader(r, t, pos);
  126.     LOOP Texts.Read(r, ch); INC(pos);
  127.         IF ch = 0X THEN EXIT END;
  128.         CASE ORD(ch) OF
  129.             128: 
  130.         | 133: Texts.Write(w, CHR(129))
  131.         | 134: Texts.Write(w, CHR(130))
  132.         | 138: Texts.Write(w, CHR(131))
  133.         | 154: Texts.Write(w, CHR(132))
  134.         | 159: Texts.Write(w, CHR(133))
  135.         | 167: Texts.WriteString(w, "ss")
  136.         ELSE
  137.         END;
  138.         IF w.buf.len > 0 THEN
  139.             Texts.Delete(t, pos-1, pos); Texts.Insert(t, pos-1, w.buf); Texts.OpenReader(r, t, pos)
  140.         END
  141. END UmlautMO;
  142. PROCEDURE UmlautMA*;    
  143.     VAR v: Viewers.Viewer; t: Texts.Text; r: Texts.Reader; ch: CHAR; pos: LONGINT;
  144. BEGIN
  145.     v := Viewers.This(Oberon.Pointer.X, Oberon.Pointer.Y);
  146.     t := v.dsc.next(TextFrames.Frame).text;
  147.     pos := 0; Texts.OpenReader(r, t, pos);
  148.     LOOP Texts.Read(r, ch); INC(pos);
  149.         IF ch = 0X THEN EXIT END;
  150.         CASE ORD(ch) OF
  151.             128:  Texts.WriteString(w, "Ae")
  152.         | 133: Texts.WriteString(w, "Oe")
  153.         | 134: Texts.WriteString(w, "Ue")
  154.         | 138: Texts.WriteString(w, "ae")
  155.         | 154: Texts.WriteString(w, "oe")
  156.         | 159: Texts.WriteString(w, "ue")
  157.         | 167: Texts.WriteString(w, "ss")
  158.         ELSE
  159.         END;
  160.         IF w.buf.len > 0 THEN
  161.             Texts.Delete(t, pos-1, pos); Texts.Insert(t, pos-1, w.buf); Texts.OpenReader(r, t, pos)
  162.         END
  163. END UmlautMA;
  164. PROCEDURE CopyList*;    
  165.  (*CopyList "oldprefix" "newprefix" file1 file2 ~*)
  166.     VAR s, from, to: ARRAY 32 OF CHAR; i: INTEGER;
  167. BEGIN
  168.     In.Open;
  169.     In.String(from); In.String(to);
  170.     WHILE In.Next() = In.name DO
  171.         In.Name(s);
  172.         Out.String(s); Out.String(" => ");
  173.         i := 0; WHILE (from[i] = s[i]) & (from[i] # 0X) DO INC(i) END;
  174.         Out.String(to); WHILE s[i] # 0X DO Out.Char(s[i]); INC(i) END;
  175.         Out.Ln
  176. END CopyList;
  177. BEGIN
  178.     Texts.OpenWriter(w)
  179. END Util.
  180. PROCEDURE Reply*;    
  181. (*puts e-mail reply marks in front of every selected line*)
  182.     VAR t: Texts.Text; beg, end, time: LONGINT; r: Texts.Reader; ch: CHAR;
  183. BEGIN
  184.     Oberon.GetSelection(t, beg, end, time);
  185.     IF time > 0 THEN
  186.         LOOP
  187.             DEC(beg);
  188.             IF beg < 0 THEN EXIT END;
  189.             Texts.OpenReader(r, t, beg); Texts.Read(r, ch);
  190.             IF ch = 0DX THEN EXIT END
  191.         END;
  192.         INC(beg);
  193.         WHILE beg < end DO
  194.             Texts.WriteString(w, "> "); Texts.Insert(t, beg, w.buf);
  195.             INC(beg, 2); INC(end, 2); Texts.OpenReader(r, t, beg);
  196.             REPEAT Texts.Read(r, ch); INC(beg) UNTIL (ch = 0DX) OR (beg >= end)
  197.         END
  198. END Reply;
  199. PROCEDURE LineLen*;    
  200.     VAR v: Viewers.Viewer; t: Texts.Text; r: Texts.Reader; ch: CHAR; n, max: INTEGER;
  201. BEGIN
  202.     v := Viewers.This(Oberon.Pointer.X, Oberon.Pointer.Y);
  203.     t := v.dsc.next(TextFrames.Frame).text;
  204.     Texts.OpenReader(r, t, 0); n := 0; max := 0;
  205.     LOOP
  206.         Texts.Read(r, ch); IF ch = 0X THEN EXIT END;
  207.         IF ch = 0DX THEN IF n > max THEN max := n END; n := 0
  208.         ELSE INC(n)
  209.         END
  210.     END;
  211.     Out.F("# characters per line$", max)
  212. END LineLen;
  213.